//
//  NavView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "NavView.h"
#import "HomeBatteryView.h"

@interface NavView()
@property (nonatomic,strong) UIButton   *backBtn;
@property (nonatomic,strong) UILabel    *infoLabel;
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic,strong) YYLabel * descLabel;
@property (nonatomic,strong) UIView * line;
@property (nonatomic,strong) UIImageView * diconView;
@property (nonatomic,strong) HomeBatteryView * batteryView;
@property (nonatomic,assign) BOOL  isRefsh;
@end

@implementation NavView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame: frame]){
        [self setSubViewLayout];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:BatteryLevelNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getContentNotification:) name:DeviceConnectionNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(geviceDataNotification:) name:UpdateDataNotification object:nil];

        
    }
    return self;
}
-(void)setSubViewLayout{
    
    [self addSubview:self.backBtn];
    [self addSubview:self.infoLabel];
    [self addSubview:self.iconView];
    [self addSubview:self.descLabel];
    
}

-(void)getNotification:(NSNotification *)notification{
    NSDictionary * infoDic = [notification object];
    [self setBolthSate:[infoDic[@"state"] boolValue]];
}

-(void)setBolthSate:(BOOL)sate{
    
    NSInteger battery = [Battery integerValue];
        if (sate) {
            [self setConnectType:YES battery:battery];
        }else{
            [self setConnectType:NO battery:0];
        }
}
//连接中
-(void)getContentNotification:(NSNotification *)notification{
    
    
    NSMutableAttributedString * attstr = [[NSMutableAttributedString alloc]initWithString:@" 连接中 "];
    NSMutableAttributedString * obj = [NSMutableAttributedString yy_attachmentStringWithContent:({
        self.line.backgroundColor = RDRGB(0x4595FF);
        self.line;
          }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(6 * FMScaleX, 6 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
    [attstr insertAttributedString:obj atIndex:0];
    self.descLabel.attributedText = attstr;
    self.descLabel.font = Regular(12);
    self.descLabel.textColor = RDRGB(0x999999);
    self.descLabel.textAlignment = NSTextAlignmentCenter;
    
    
}

//更新数据
-(void)geviceDataNotification:(NSNotification *)notification{
    
    NSMutableAttributedString * attstr = [[NSMutableAttributedString alloc]initWithString:@" 更新数据 "];
    NSMutableAttributedString * obj = [NSMutableAttributedString yy_attachmentStringWithContent:({
        self.line.backgroundColor = RDRGB(0xFBD058);
        self.line;
    }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(6 * FMScaleX, 6 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
    [attstr insertAttributedString:obj atIndex:0];
    self.descLabel.attributedText = attstr;
    self.descLabel.font = Regular(12);
    self.descLabel.textColor = RDRGB(0x999999);
    self.descLabel.textAlignment = NSTextAlignmentCenter;
    
}

-(void)setConnectType:(BOOL)type battery:(NSInteger )battery{
        
    if (!type) {
        NSMutableAttributedString * attstr = [[NSMutableAttributedString alloc]initWithString:@" 设备离线 "];
        NSMutableAttributedString * obj = [NSMutableAttributedString yy_attachmentStringWithContent:({
            self.line.backgroundColor = RDRGB(0xFBD058);
            self.line;
              }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(6 * FMScaleX, 6 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
        [attstr insertAttributedString:obj atIndex:0];
        NSMutableAttributedString * obj1 = [NSMutableAttributedString yy_attachmentStringWithContent:({
            self.diconView;
              }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(8 * FMScaleX, 12 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
        [attstr insertAttributedString:obj atIndex:0];
        [attstr insertAttributedString:obj1 atIndex:attstr.length];

        self.descLabel.attributedText = attstr;
        self.descLabel.font = Regular(12);
        self.descLabel.textColor = RDRGB(0x999999);
        self.descLabel.textAlignment = NSTextAlignmentCenter;
    }else{
        
        NSMutableAttributedString * attstr;
        if (battery == 101) {
            attstr = [[NSMutableAttributedString alloc]initWithString:@" 充电中 "];
        }else{
            attstr = [[NSMutableAttributedString alloc]initWithString:@" 设备在线 "];
        }
        [self.batteryView setBattery:battery];
        NSMutableAttributedString * obj = [NSMutableAttributedString yy_attachmentStringWithContent:({
            self.line.backgroundColor = RDRGB(0x27C824);
            self.line;
              }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(6 * FMScaleX, 6 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
        [attstr insertAttributedString:obj atIndex:0];
        NSMutableAttributedString * obj1 = [NSMutableAttributedString yy_attachmentStringWithContent:({
            self.batteryView;
              }) contentMode:UIViewContentModeRight attachmentSize:CGSizeMake(60 * FMScaleX, 22 * FMScaleX) alignToFont:Regular(12) alignment:YYTextVerticalAlignmentCenter];
        [attstr insertAttributedString:obj atIndex:0];
        [attstr insertAttributedString:obj1 atIndex:attstr.length];

        self.descLabel.attributedText = attstr;
        self.descLabel.font = Regular(12);
        self.descLabel.textColor = RDRGB(0x999999);
        self.descLabel.textAlignment = NSTextAlignmentCenter;
    }
}

-(void)setTitleText:(NSString *)titleText{
    
    self.infoLabel.text = titleText;
    self.iconView.hidden = YES;
    self.descLabel.hidden = YES;
}

-(void)hideInfo{
    
    self.iconView.hidden = YES;
    self.descLabel.hidden = YES;
    self.infoLabel.hidden = YES;
    self.backBtn.hidden = YES;
}


-(UIButton *)backBtn{
    
    if(!_backBtn){
        _backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, StatusBar, 44, 44)];
        [_backBtn setImage:[UIImage imageNamed:@"Home_info_back"] forState:UIControlStateNormal];
    }
    return _backBtn;
}
-(UILabel *)infoLabel{
    
    if(!_infoLabel){
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, StatusBar, WIDTH - 200, NavHeight - StatusBar)];
        _infoLabel.textColor = UIColor.blackColor;
        _infoLabel.font = Semibold(16);
        _infoLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _infoLabel;
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake((WIDTH - 120 * FMScaleX) / 2, StatusBar, 120 * FMScaleX, 18 * FMScaleX)];
        _iconView.image = [UIImage imageNamed:@"Home_titlte_icon"];
    }
    return _iconView;
}

-(YYLabel *)descLabel{
    
    if (!_descLabel) {
        _descLabel = [[YYLabel alloc]initWithFrame:CGRectMake((WIDTH - 180 * FMScaleX) / 2, GETMaxY(self.iconView), 180 * FMScaleX, 22 * FMScaleX)];
        _descLabel.font = Regular(12);
        _descLabel.textColor = RDRGB(0x999999);
        _descLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _descLabel;
}
-(UIView *)line{
    
    if (!_line) {
        _line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 6 * FMScaleX, 6 * FMScaleX)];
        [_line setCornerRadius:3 * FMScaleX];
        _line.backgroundColor = RDRGB(0xFBD058);
    }
    return _line;
}
-(UIImageView *)diconView{
    
    if (!_diconView) {
        _diconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 8 * FMScaleX, 12 * FMScaleX)];
        _diconView.image = [UIImage imageNamed:@"Home_Ble_Disconnect"];
    }
    return _diconView;
}
-(HomeBatteryView *)batteryView{
    
    if (!_batteryView) {
        _batteryView = [[HomeBatteryView alloc]initWithFrame:CGRectMake(0, 0, 60 * FMScaleX, 22 * FMScaleX)];
    }
    return _batteryView;
}
@end
